package com.zw.mes.module.assemble.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.sys.Dept;
import com.zw.mes.module.assemble.entity.AsseAssemblySheet;
import com.zw.mes.module.assemble.mapper.DispatchMapper;
import com.zw.mes.module.assemble.model.CraMakeModel;
import com.zw.mes.module.assemble.model.ProStructModel;
import com.zw.mes.module.assemble.model.ProdOrdModel;
import com.zw.mes.module.assemble.repository.AsseAssemblySheetRepository;
import com.zw.mes.module.assemble.repository.AsseProcessRepository;
import com.zw.mes.module.assemble.repository.AsseRouteRepository;
import com.zw.mes.module.assemble.service.AssMakeService;
import com.zw.mes.module.assemble.service.DispatchService;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.system.mapper.DeptMapper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 装配派工单 增删改查、提交、审核等功能
 * <p>
 * 权限：
 * @Author: China.wsg
 * @Date: 2021/04/16 16:12
 */
@Controller
@RequestMapping("/assemble/dispatch")
@SuppressWarnings("all")
public class DispatchController extends BaseController {

    @Autowired
    private DispatchService dispatchService;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private AsseRouteRepository asseRouteRepository;
    @Autowired
    private AsseProcessRepository asseProcessRepository;
    @Autowired
    private AsseAssemblySheetRepository assemblySheetRepository;
    @Autowired
    private DispatchMapper dispatchMapper;
    @Autowired
    private AssMakeService makeService;

    @GetMapping("/addPage")
    @RequiresPermissions({"asseOrd:est:view"})
    public String addPage(Model model) {
//        model.addAttribute("depts", deptMapper.findByCondition(new Dept()));
        model.addAttribute("routes", asseRouteRepository.findByIsDelOrderByOrderNoAsc(0));
        model.addAttribute("processes", asseProcessRepository.findByIsDelOrderByOrderNoAsc(0));
        return "assemble/dispatch/dispatchAdd";
    }

    /**
     * 部门
     */
    @ResponseBody
    @PostMapping("/deptList")
    public JsonResult deptList() {
        startPage();
        List<Dept> list = deptMapper.findByCondition(new Dept());
        return jsonResult(list);
    }

    @GetMapping("/addAmsPage")
    @RequiresPermissions({"asseOrd:est:view"})
    public String addAmsPage(Model model) {
        model.addAttribute("depts", deptMapper.findByCondition(new Dept()));
        model.addAttribute("routes", asseRouteRepository.findByIsDelOrderByOrderNoAsc(0));
        model.addAttribute("processes", asseProcessRepository.findByIsDelOrderByOrderNoAsc(0));
        return "assemble/dispatch/dispatchAmsAdd";
    }

    /**
     * 获取生产令列表数据
     *
     * @param q
     * @return
     */
    @GetMapping("/prodOrdList")
    @ResponseBody
    @RequiresPermissions({"asseOrd:est:view"})
    public List<ProdOrdModel> prodOrdListByCode(String q) {
        return dispatchService.prodOrdListByCode(q);
    }

    /**
     * 查询项目部分
     */
    @GetMapping("/partList")
    @ResponseBody
    @RequiresPermissions({"asseOrd:est:view"})
    public List<ProStructModel> partList(String code, String type) {
        return dispatchService.findPartByProCodeAndSpecial(code, type);
    }

    /**
     * 查询图号列表
     */
    @GetMapping("/drawNoList")
    @ResponseBody
    public List<CraMakeModel> drawNoList(String code, String special, Integer partId, String drawNo) {
        return dispatchService.findCarMakeListByCodeAndPartId(code, special, partId, drawNo);
    }

    /**
     * 查询图号
     */
    @GetMapping("/drawNo")
    @ResponseBody
    public CraMakeModel drawNo(Integer id) {
        return dispatchService.findDrawNoById(id);
    }

    /**
     * 保存装配派工单
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"asseOrd:est:edit"})
    public JsonResult save(AsseAssemblySheet assemblySheet, String[] route, Integer[] orderNo, String[] process, String[] comment) {
        JsonResult jsonResult = jsonResult(dispatchService.save(assemblySheet, route, orderNo, process, comment));
        if (jsonResult.getType() == JsonResult.ResultType.ERROR) {
            jsonResult.setMsg("装配派工单已存在，请修改");
        }
        return jsonResult;
    }

    /**
     * 提交列表页面
     *
     * @return
     */
    @GetMapping("/subPage")
    @RequiresPermissions({"asseOrd:submit:view"})
    public String subPage() {
        return "assemble/dispatch/dispatchSub";
    }

    /**
     * 提交列表数据
     *
     * @param assemblySheet
     * @return
     */
    @ResponseBody
    @PostMapping("/subPageList")
    @RequiresPermissions({"asseOrd:submit:view"})
    public JsonResult subPageList(AsseAssemblySheet assemblySheet) {
        startPage();
        List<AsseAssemblySheet> list = dispatchService.findAssemblySheetById(assemblySheet);
        return jsonResult(list);
    }

    /**
     * 删除装配派工单
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"asseOrd:submit:del"})
    public JsonResult delAsseSheet(Integer id) {
        return jsonResult(dispatchService.delAsseSheetById(id));
    }

    /**
     * 提交装配派工单
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/commit")
    @RequiresPermissions({"asseOrd:submit:commit"})
    public JsonResult subAsseSheet(Integer id) {
        return jsonResult(dispatchService.subAsseSheetById(id) == null ? 0 : 1);
    }

    /**
     * 撤销装配派工单
     *
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"asseOrd:submit:commit"})
    public JsonResult cancelAsseSheet(Integer id) {
        return jsonResult(dispatchService.cancelAsseSheetById(id) == null ? 0 : 1);
    }

    /**
     * 查询列表页面
     *
     * @return
     */
    @GetMapping("/selPage")
    @RequiresPermissions({"asseOrd:list:view"})
    public String selPage() {
        return "assemble/dispatch/dispatchSel";
    }

    /**
     * 查询列表数据
     *
     * @param assemblySheet
     * @return
     */
    @ResponseBody
    @PostMapping("/selPageList")
    @RequiresPermissions({"asseOrd:list:view"})
    public JsonResult selPageList(AsseAssemblySheet assemblySheet) {
        startPage();
        List<AsseAssemblySheet> list = dispatchService.findAssemblySheetById(assemblySheet);
        return jsonResult(list);
    }

    /**
     * 修改页面
     *
     * @param id
     * @return
     */
    @GetMapping("/editPage/{id}")
    @RequiresPermissions({"asseOrd:submit:edit"})
    public String editPage(@PathVariable("id") Integer id, Model model) {
        AsseAssemblySheet asse = assemblySheetRepository.getOne(id);
        model.addAttribute("asse", asse);
        model.addAttribute("details", dispatchMapper.findAssemblyDetailForEditBySheetId(id));
//        model.addAttribute("depts", deptMapper.findByCondition(new Dept()));
        model.addAttribute("deptList", deptMapper.findDeptByIds(asse.getDeptId()));
        model.addAttribute("parts", dispatchService.findPartByProCodeAndSpecial(asse.getProCode(), asse.getSpecial()));
        model.addAttribute("drawNos", dispatchMapper.findCarMakeListByCodeAndPartId(asse.getProCode(), asse.getSpecial(), asse.getPartId(), asse.getDrawNo()));
        model.addAttribute("routes", dispatchMapper.findRoutesNotTransBySheetId(id));
        model.addAttribute("processes", asseProcessRepository.findByIsDelOrderByOrderNoAsc(0));
        model.addAttribute("transFlg", dispatchMapper.getTransFlgBySheetId(id));
        model.addAttribute("buyList", makeService.findAsseAssemblyBuyList(id));
        return "assemble/dispatch/dispatchEdit";
    }

    /**
     * 修改页面（自动化）
     *
     * @param id
     * @return
     */
    @GetMapping("/editAmsPage/{id}")
    @RequiresPermissions({"asseOrd:submit:edit"})
    public String editAmsPage(@PathVariable("id") Integer id, Model model) {
        AsseAssemblySheet asse = assemblySheetRepository.getOne(id);
        model.addAttribute("asse", asse);
        model.addAttribute("depts", deptMapper.findByCondition(new Dept()));
        model.addAttribute("parts", dispatchService.findPartByProCodeAndSpecial(asse.getProCode(), asse.getSpecial()));
        model.addAttribute("buyList", makeService.findAsseAssemblyBuyList(id));
        return "assemble/dispatch/dispatchAmsEdit";
    }

    /**
     * 修改装配派工单
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"asseOrd:submit:edit"})
    public JsonResult edit(AsseAssemblySheet assemblySheet, String[] route, Integer[] orderNo, String[] process, String[] comment) {
        JsonResult jsonResult = jsonResult(dispatchService.edit(assemblySheet, route, orderNo, process, comment));
        if (jsonResult.getType() == JsonResult.ResultType.ERROR) {
            jsonResult.setMsg("装配派工单存在转序，请关闭该页面后再次打开，进行编辑");
        }
        return jsonResult;
    }

    /**
     * 查看页面
     *
     * @param id
     * @return
     */
    @GetMapping("/viewPage/{id}")
    @RequiresPermissions(value = {"asseOrd:submit:view", "asseOrd:list:view"}, logical = Logical.OR)
    public String viewPage(@PathVariable("id") Integer id, Model model) {
        AsseAssemblySheet asse = assemblySheetRepository.getOne(id);
        model.addAttribute("asse", asse);
        model.addAttribute("details", dispatchService.findAssemblyDetailBySheetId(id));
        return "assemble/dispatch/dispatchView";
    }

    /**
     * @Author wsg
     * @Date 13:20 2021/5/11
     * @Description 自动化装配任务反馈 页面
     **/
    @GetMapping("/bakcPage")
    @RequiresPermissions({"asseOrd:eleBack:view"})
    public String bakcPage() {
        return "assemble/dispatch/dispatchBack";
    }

    /**
     * @Author wsg
     * @Date 10:29 2021/6/9
     * @Description 自动化装配任务反馈 列表
     **/
    @ResponseBody
    @PostMapping("/backPageList")
    @RequiresPermissions(value = {"asseOrd:eleBack:view", "overview:home:view"}, logical = Logical.OR)
    public JsonResult backPageList(AsseAssemblySheet assemblySheet) {
        startPage();
        List<AsseAssemblySheet> list = dispatchService.findAssemblySheeteElectric(assemblySheet);
        return jsonResult(list);
    }

    /**
     * @Author wsg
     * @Date 14:10 2021/6/9
     * @Description 自动化装配进度反馈
     **/
    @ResponseBody
    @PostMapping("/eleBakeSave")
    @RequiresPermissions({"asseOrd:eleBack:edit"})
    public JsonResult eleBakeSave(AsseAssemblySheet assemblySheet) {
        return jsonResult(dispatchService.saveEleBake(assemblySheet));
    }


}
